package com.neusoft.interceptor;

import com.neusoft.exception.AuthFailException;
import com.neusoft.mapper.UsersMapper;
import com.neusoft.model.Users;
import com.neusoft.utils.JwtService;
import com.neusoft.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    JwtService jwtService;
    @Autowired
    UsersMapper usersMapper;

    private void AuthFail() throws AuthFailException {
        throw new AuthFailException("认证失败");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String url = request.getRequestURI();
        String method = request.getMethod();
        String token = request.getHeader("Authorization");
        if (token != null) {
            token = token.replace("Token ", "");
            if (jwtService.validateToken(token)) {
                String userId = jwtService.getSub(token);
                Users users = usersMapper.selectById(Integer.parseInt(userId));
                if (users != null) {
                    UserUtils.setLoginUser(users);
//                    System.out.println(users.toString());
                    return true;

                } else {
                    AuthFail();
                    return false;
                }
            } else {
                AuthFail();
                return false;
            }
        } else {
            if (method.equals("GET") && url.startsWith("/api/articles")) {
                if (url.startsWith("/api/articles/feed")) {
                    AuthFail();
                    return false;
                } else {
                    return true;
                }
            }
            if (method.equals("GET") && url.startsWith("/api/profiles")) {
                return true;
            }
            AuthFail();
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        UserUtils.removeUser();
        System.out.println("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }

}